//
//  NSString+QWExtension.m
//  WJCommon-master
//
//  Created by Yunhai.Wu on 15/12/16.
//  Copyright © 2015年 WJ. All rights reserved.
//

#import "NSString+QWCalculateSize.h"


@implementation NSString (QWCalculateSize)

-(CGSize)qw_sizeWithFont:(UIFont *)font width:(CGFloat)width mode:(NSLineBreakMode)mode {
    return [self qw_sizeWithFont:font width:width mode:mode lineSpacing:0];
}

-(CGSize)qw_sizeWithFont:(UIFont *)font width:(CGFloat)width mode:(NSLineBreakMode)mode lineSpacing:(CGFloat)spacing {
    CGSize s = CGSizeZero;
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_7_0
    s = [self sizeWithFont:font constrainedToSize:CGSizeMake(width, MAXFLOAT) lineBreakMode:mode];
#else
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = mode;
    if (spacing > 0) {
        [paragraphStyle setLineSpacing:spacing];
    }
    NSDictionary *attributes = [[NSDictionary alloc] initWithObjectsAndKeys:font,NSFontAttributeName,paragraphStyle,NSParagraphStyleAttributeName, nil];
    s = [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
#endif
    return s;
}

@end
